﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Threading;

using Core.Common;

namespace ShutDownTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);

            
            while (ShutDownState == 0)
            {
                LogTextHelper.WriteLine("主线程休眠30秒");
                Thread.Sleep(30 * 1000);
                Interlocked.CompareExchange(ref ShutDownState, 2, 1);
            }
            LogTextHelper.WriteLine("主线程正常退出");
        }

        //下面是系统注销或关闭事件处理程序， 
        static volatile int ShutDownState = 0;
        private static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
        {
            Interlocked.CompareExchange(ref ShutDownState, 1, 0);
            while (ShutDownState == 1)
            {
                LogTextHelper.WriteLine("关机线程休眠1秒");
                Thread.Sleep(1000);
            }
            LogTextHelper.WriteLine("关机线程退出");
        }
    }
}
